﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HouseHoldingFileRepository.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the HouseHoldingFileRepository type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace FileRepository.Repositories
{
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using SystemFileAdapter;
  using Entities;
  using FileRepository.Interfaces;
  using FileSystemInterfaces;

  public class HouseHoldingFileRepository : BaseFileRepository, IHouseHoldingFileRepository
  {
    public HouseHoldingFileRepository(String path, IFileInfoFactory fileInfo, IDirectoryInfo directoryInfo)
      : base(path, fileInfo, directoryInfo)
    {
    }

    public HouseHoldingRunData GetHouseHoldingData()
    {
      var houseHoldingRun = new HouseHoldingRunData();

      foreach (FileInfo file in this.Entities)
      {
        String[] lines = File.ReadAllLines(file.FullName);

        List<Row> csvLines = (from l in lines 
                     let data = l.Split(',') 
                     select new Row
                              {
                                ColumnOne = data[0],
                                ColumnTwo = data[1],
                                ColumnThree = data[2]
                              }).ToList();

        Boolean firstRow = true;
        foreach (Row row in csvLines)
        {
          if (firstRow)
          {
            houseHoldingRun.Grid = row.ColumnOne;
            houseHoldingRun.StartDate = Convert.ToDateTime(row.ColumnTwo);
            houseHoldingRun.EndDate = Convert.ToDateTime(row.ColumnThree);

            firstRow = false;
          }
          else
          {
            houseHoldingRun.DocumentRunData.Add(new DocumentRunData
                                                  {
                                                    DocumentId = row.ColumnTwo,
                                                    HouseHoldDate = Convert.ToDateTime(row.ColumnThree)
                                                  });

            if (!houseHoldingRun.ProcessingGridRun.Contains(row.ColumnOne))
            {
              houseHoldingRun.ProcessingGridRun.Add(row.ColumnOne);
            }
          }
        }
      }

      return houseHoldingRun;
    }

    protected override String GenerateFileName()
    {
      return this.GenerateFileName("filename");
    }

    private String GenerateFileName(String id)
    {
      return string.Format("{0}/{1}.csv", this.Path, id);
    }

    private class Row
    {
      public String ColumnOne { get; set; }

      public String ColumnTwo { get; set; }

      public String ColumnThree { get; set; }
    }
  }
}
